﻿// https://mp.weixin.qq.com/s?__biz=MzUxNjY5NTYxNA==&mid=2247488822&idx=2&sn=f431fba72493f4527d379bd077364a54&scene=21#wechat_redirect
// https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/


#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *dummy_head = new ListNode(0);
        dummy_head->next = head;
        
        ListNode *fast = dummy_head;
        ListNode *slow = dummy_head;
        ListNode *temp;

        while (n--) {
            fast = fast->next;
        }

        while (fast->next != nullptr) {
            fast = fast->next;
            slow = slow->next;
        }

        temp = slow->next;
        slow->next = slow->next->next;
        delete temp;
        return dummy_head->next;
    }
};

int main() {
    Solution obj = Solution();
}